# -*- coding: utf-8  -*-
#!/usr/local/bin/python

__author__ = 'jian.yao'

from django.db import models

class CommodityManager(models.Manager):
    def create_commodity(self, name, **kwargs):

        commodity = self.model(name=name)
        if "img_url" in kwargs:
            commodity.img_url = kwargs["img_url"]
        if "price" in kwargs:
            commodity.price = kwargs["price"]
        if "stock" in kwargs:
            commodity.stock = kwargs["stock"]
        if "desc" in kwargs:
            commodity.description = kwargs["desc"]
        if "catagory" in kwargs:
            commodity.catagory = kwargs["catagory"]

        commodity.save(using = self._db)
        return commodity

class CommodityCatagory(models.Model):
    type            = models.CharField(max_length=128, null=False, blank=False)
    create_time     = models.DateTimeField(auto_now_add=True, db_index=True)

class Commodity(models.Model):
    name            = models.CharField(max_length=512, null=False, blank=False)
    img_url         = models.URLField(default="/", null=False)
    price           = models.FloatField(default=0.0, null=False)
    stock           = models.IntegerField(default=0, null=False)
    description     = models.CharField(max_length=1024, null=False, blank=False)
    catagory        = models.CharField(max_length=128, null=False, blank=False)
    create_time     = models.DateTimeField(auto_now_add=True, db_index=True)
    last_update     = models.DateTimeField(auto_now=True)

    objects = CommodityManager()




